468. Validate IP Address - LeetCode Solution


String

Python Code:

class Solution:
    def validIPAddress(self, IP: str) -> str:
        def ipv4(ip):
            s=ip.split('.')
            if len(s)!=4:
                return False
            for e in s:
                try:
                    v=int(e)
                    if v>255 or e[0]=='-' or str(v)!=e:
                        return False
                except:
                    return False
            return True
        
        def ipv6(ip):
            s=ip.split(':')
            if len(s)!=8:
                return False
            
            for e in s:
                l=len(e)
                if l>4 or l==0:
                        return False
                try:
                    v=int(e,16)
                    if e[0] == '-':
                        return False
                    
                except:
                    return False
            return True
        
        ip=IP
               
        if '.' in ip and ipv4(ip):
            return 'IPv4'
        if ':' in ip and ipv6(ip):
            return 'IPv6'
            
        return 'Neither'


Comments

Submit
0 Comments
More Questions

Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree